# -*- coding: utf-8 -*-
import scrapy
from copy import deepcopy
from scrapy_redis.spiders import RedisSpider


class KpSpider(RedisSpider):
    name = 'kp'
    allowed_domains = ['gotokeep.com']
    # start_urls = ['http://www.gotokeep.com/training']
    redis_key = 'keep'
    def parse(self, response):
        #大分类
        li_list=response.xpath("//div[@class='keep-wrapper training']/ul/li")
        for li in li_list:
            item={}
            item["url1"]=li.xpath("./a/@href").extract_first()
            item["title1"]=li.xpath("./a/div/div[1]")
            item["count"]=li.xpath("./a/div/div[2]")
            if item["url1"] is not None:
                item["url1"]="http://www.gotokeep.com"+item["url1"]
                yield scrapy.Request(
                    item["url1"],
                    callback="parse_keep_list",
                    meta={"item":deepcopy(item)}
                )
    def parse_keep_list(self,response):
        item=response.meta["item"]
        li_list=response.xpath("//div[@class='keep-wrapper']/ul/li")
        for li in li_list:
            item["url2"]=li.xpath("./a/@href").extract_first()
            item["title2"]=li.xpath("./a/div[@class='name']/text()")


